package com.corpdata.app.test.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.corpdata.common.utils.HttpUtils;
import com.google.gson.Gson;
/**
 * 00000002  	37个门
 * 00000012		37个门	
 * @author XY
 * 富地大厦
 * F0000088  高斯
 * F0000090  牛顿
 * F0000087  所有
 *
 */

public class Main {
	//开始：09:48---
	public static void main(String[] args) throws Exception {//15:21---
		String token = "bc5ff82289a24c4cb31707a5ff8247b51540953710797";
		getApi("3c.visitor.invite", "F0000087" , token);//访客二维码
//		getApi("3c.door.querydoors","F0000088", token);//门禁设备  
		
	}

	private static void getApi(String serviceId, String personCode, String token)
			throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException, ClientProtocolException {
		// 接口地址
		String url = "http://www.jslife.com.cn/jsaims/as";
		// 客户号
		String cid = "880001001011766";
		// 接口版本号
		String v = "2";
		// 邀访接口
		String p = "{\"serviceId\":\"" + serviceId+ "\",\"requestType\":\"DATA\","
				+ "\"attributes\":{\"areaCode\":\"0000011678\",\"personCode\":\""+ personCode+ "\","
				+ "\"visitorType\":0,\"businesserCode\":\"52\",\"parkCode\":\"0000011678\","
				+ "\"timeDesc\":{\"sd\":\"2018-08-07 15:00:00\",\"ed\":\"2018-12-10 15:00:00\","
				+ "\"st\":\"00:00\",\"et\":\"23:59\",\"w\":\"1111111\"}},"
				+ "\"dataItems\":[{\"attributes\":{\"visitorName\":\"456测试\",\"visitorTel\":\"18303099999\"}}]}";
		
		//人员门禁设备协议
		/*String p ="{" + 
				"\"serviceId\":\"3c.door.querydoors\"," + 
				"\"requestType\":\"DATA\"," + 
				"\"attributes\":{ " + 
				"				\"areaCode\": \"0000011678\"," + 
				"				\"personCode\": \""+personCode+"\" " + 
				"}} " ;*/
		// 签名signKey
		String signKey = "6a6235c88bf82d454578582cac2ca8d7";
		// 生成MD5签名
		MessageDigest md5Tool = MessageDigest.getInstance("MD5");
		byte[] md5Data = md5Tool.digest((p + signKey).getBytes("UTF-8"));
		String sn = toHexString(md5Data);

		// 构造参数
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("cid", cid));
		list.add(new BasicNameValuePair("v", v));
		list.add(new BasicNameValuePair("p", p));
		list.add(new BasicNameValuePair("sn", sn));
		list.add(new BasicNameValuePair("tn", token));
		HttpEntity en = new UrlEncodedFormEntity(list, Consts.UTF_8);
		post.setEntity(en);

		// 发送消息和处理结果
		CloseableHttpResponse response = client.execute(post);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			System.out.println("执行成功！");
			String s = EntityUtils.toString(response.getEntity());
			System.out.println(s);
			JSONObject jo = JSONObject.parseObject(s);
			if(jo.getString("resultCode").equals("0")) {
				JSONArray arr = JSONArray.parseArray(jo.get("dataItems").toString());
				for(int i = 0 ; i < arr.size() ;i++) {
					JSONObject o  = JSONObject.parseObject(arr.get(i).toString());
					System.out.println(o.get("attributes").toString());
				}
			}
		} else {
			System.out.println("执行失败！");
		}
		response.close();
	}

	private static String toHexString(byte[] bytes) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			buffer.append(String.format("%02X", bytes[i]));
		}
		return buffer.toString();
	}

}
